package com.zmzncs.lmtc.module.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.common.pojo.enums.PayTypeEnum;
import com.zmzncs.lmtc.common.util.BolianUtil;
import com.zmzncs.lmtc.common.pojo.bolian.BolianOrderHistoryRes;
import com.zmzncs.lmtc.common.util.DateTimeUtil;
import com.zmzncs.lmtc.common.util.EnumUtil;
import com.zmzncs.lmtc.module.order.dao.OrderInfoDao;
import com.zmzncs.lmtc.module.order.dao.OrderTradeRecordDao;
import com.zmzncs.lmtc.module.order.pojo.order_Info.BolianOrderHistoryForm;
import com.zmzncs.lmtc.module.order.pojo.order_Info.OrderInfo;
import com.zmzncs.lmtc.module.order.pojo.order_trade_record.OrderTradeRecord;
import com.zmzncs.lmtc.module.order.pojo.order_trade_record.OrderTradeRecordDTO;
import com.zmzncs.lmtc.module.order.service.OrderTradeRecordService;
import com.zmzncs.lmtc.module.park.dao.ParkInfoDao;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfo;
import com.zmzncs.lmtc.module.park.pojo.park_info.ParkInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单-订单交易记录 服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-09-24
 */
@Slf4j
@Service
public class OrderTradeRecordServiceImpl extends ServiceImpl<OrderTradeRecordDao, OrderTradeRecord> implements OrderTradeRecordService {

    @Autowired
    OrderTradeRecordDao orderTradeRecordDao;
    @Autowired
    OrderInfoDao orderInfoDao;
    @Autowired
    ParkInfoDao parkInfoDao;


    /**
     * 根据订单号查询订单交易记录
     */
    @Override
    public List<OrderTradeRecordDTO> getOrderTradeRecordByOrderCode(String orderCode) {
        List<OrderTradeRecordDTO> orderTradeRecordDTOList = orderTradeRecordDao.getOrderTradeRecordByOrderCode(orderCode);
        for(OrderTradeRecordDTO tradeRecordDTO: orderTradeRecordDTOList){
            if (tradeRecordDTO.getPayType() != null) {
                PayTypeEnum payTypeEnum = EnumUtil.getByCode(tradeRecordDTO.getPayType(), PayTypeEnum.class);
                if(payTypeEnum != null){
                    tradeRecordDTO.setPayTypeText(payTypeEnum.getMassage());
                }
            }

        }
        return orderTradeRecordDTOList;
    }

    /**
     * 根据订单编号查询泊链历史订单
     */
    @Override
    public BolianOrderHistoryRes getBolianOrderByOrderCode(String orderCode) {
        OrderInfo orderInfo = orderInfoDao.selectByOrderCode(orderCode);
        if (orderInfo == null) {
            log.info("订单号查询泊链订单历史记录，未查询到订单信息");
            return null;
        }
        ParkInfo parkInfo = parkInfoDao.selectById(orderInfo.getParkId());
        if (parkInfo == null){
            log.info("订单号查询泊链订单历史记录，未查询到停车场信息");
            return null;
        }
        String bolianParkUnionId = parkInfo.getBolianParkUnionId();
        String bolianParkId = parkInfo.getBolianParkId();
        String carNumber = orderInfo.getCarNumber();
        Date inTime = orderInfo.getInTime();
        Date outTime = orderInfo.getOutTime();
        Date date = new Date();
        inTime = inTime == null ? date : inTime;
        outTime = outTime == null ? date : outTime;
        Long start = DateTimeUtil.getStartOfDay(inTime).getTime() / 1000;
        Long end = DateTimeUtil.getEndOfDay(outTime).getTime() / 1000;

        BolianOrderHistoryRes bolianOrderHistoryRes = BolianUtil.queryOrders(orderCode,bolianParkUnionId,bolianParkId,carNumber,start,end,0,30);
        return bolianOrderHistoryRes;
    }

    @Override
    public BolianOrderHistoryRes getBolianOrderHistory(BolianOrderHistoryForm form) {

        String orderCode = form.getOrderCode();
        String carNum = form.getCarNum();

        Date startTime = form.getStartTime();
        Date endTime = form.getEndTime();
        Date date = new Date();
        Date startOfDay = DateTimeUtil.getStartOfDay(date);
        Date endOfDay = DateTimeUtil.getEndOfDay(date);
        startTime = startTime == null ? startOfDay : startTime;
        endTime = endTime == null ? endOfDay : endTime;
        Long start = startTime.getTime() / 1000;
        Long end = endTime.getTime() / 1000;

        Integer size = form.getSize();
        size = size == null ? 0 : size;
        Integer page = form.getPage();
        page = page == null ? 30 : page;

        String parkUnionId = form.getParkUnionId();
        String parkId = form.getParkId();
        if (parkUnionId == null || parkId == null){
            ParkInfoDTO park = parkInfoDao.getParkByBolianComId(form.getComId());
            if (park != null){
                parkUnionId = park.getBolianParkUnionId();
                parkId = park.getBolianParkId();
            }
        }

        BolianOrderHistoryRes bolianOrderHistoryRes = BolianUtil.queryOrders(orderCode, parkUnionId, parkId, carNum, start, end, page, size);
        return bolianOrderHistoryRes;
    }

}
